﻿package test.ui {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import gear.core.Game;
	import gear.net.AssetData;
	import gear.net.LibData;
	import gear.net.RESManager;
	import gear.net.SWFLoader;
	import gear.ui.controls.GButton;
	import gear.ui.controls.GGrid;
	import gear.ui.core.ScaleMode;
	import gear.ui.data.GButtonData;
	import gear.ui.data.GGridData;
	import gear.ui.skin.tb.MCToggleButtonSkin;


	/**
	 * @author admin
	 */
	[SWF(width=500,height=400,backgroundColor=0x333333,frameRate=48)]
	public class TestGHGrid extends Game {
		private var _hgrid : GGrid;
		private var _first_page_btn : GButton;
		private var _last_page_btn : GButton;
		private var _prev_page_btn : GButton;
		private var _next_page_btn : GButton;
		private var _prev_one_btn : GButton;
		private var _next_one_btn : GButton;

		override protected function startup() : void {
			RESManager.instance.add(new SWFLoader(new LibData("assets/ui.swf", "ui")));
			RESManager.instance.addEventListener(Event.COMPLETE, completeHandler);
			RESManager.instance.load();
		}

		private function completeHandler(event : Event) : void {
			RESManager.instance.removeEventListener(Event.COMPLETE, completeHandler);
			addGHGrid();
			addGButtons();
			addEvents();
			// _hgrid.pageModel.firstPage();
			_hgrid.model.source = [1, 2, 3];
			_hgrid.model.limit = 3;
			_hgrid.model.max = 15;
		}

		private function addGButtons() : void {
			var data : GButtonData = new GButtonData();
			data.x = 10;
			data.y = 100;
			data.labelData.text = "prev one";
			_prev_one_btn = new GButton(data);
			addChild(_prev_one_btn);
			data = data.clone();
			data.x = 10;
			data.y = 140;
			data.labelData.text = "prev step";
			_prev_page_btn = new GButton(data);
			addChild(_prev_page_btn);
			data = data.clone();
			data.x = 10;
			data.y = 180;
			data.labelData.text = "first";
			_first_page_btn = new GButton(data);
			addChild(_first_page_btn);
			data = data.clone();
			data.x = 100;
			data.y = 100;
			data.labelData.text = "next one";
			_next_one_btn = new GButton(data);
			addChild(_next_one_btn);
			data = data.clone();
			data.x = 100;
			data.y = 140;
			data.labelData.text = "next step";
			_next_page_btn = new GButton(data);
			addChild(_next_page_btn);
			data = data.clone();
			data.x = 100;
			data.y = 180;
			data.labelData.text = "last";
			_last_page_btn = new GButton(data);
			addChild(_last_page_btn);
		}

		private function addGHGrid() : void {
			var data : GGridData = new GGridData();
			data.columns = 5;
			data.rows = 1;
			data.cell = TestCell;
			data.cellData.skin = new MCToggleButtonSkin(RESManager.getMC(new AssetData("testCell")));
			data.cellData.scaleMode = ScaleMode.NONE;
			_hgrid = new GGrid(data);
			// _hgrid.pageModel = new SyncPageModel(TestSyncPageData);
			addChild(_hgrid);
		}

		private function addEvents() : void {
			_prev_page_btn.addEventListener(MouseEvent.CLICK, prevPage_clickHandler);
			_prev_one_btn.addEventListener(MouseEvent.CLICK, prev_clickHandler);
			_next_one_btn.addEventListener(MouseEvent.CLICK, next_clickHandler);
			_next_page_btn.addEventListener(MouseEvent.CLICK, nextPage_clickHandler);
			_first_page_btn.addEventListener(MouseEvent.CLICK, firstPage_clickHandler);
			_last_page_btn.addEventListener(MouseEvent.CLICK, lastPage_clickHandler);
		}

		private function lastPage_clickHandler(event : MouseEvent) : void {
			_hgrid.pageModel.last();
		}

		private function firstPage_clickHandler(event : MouseEvent) : void {
			_hgrid.pageModel.first();
		}

		private function nextPage_clickHandler(event : MouseEvent) : void {
			_hgrid.pageModel.nextPage();
		}

		private function prevPage_clickHandler(event : MouseEvent) : void {
			_hgrid.pageModel.prevPage();
		}

		private function prev_clickHandler(event : MouseEvent) : void {
			_hgrid.pageModel.prevMove();
		}

		private function  next_clickHandler(event : MouseEvent) : void {
			_hgrid.pageModel.nextMove();
		}
	}
}
